package cn.kgc.biz.controller;

import cn.kgc.biz.service.UserService;
import cn.kgc.biz.vo.LinkUserNameVo;
import cn.kgc.biz.vo.LinkUserVo;
import cn.kgc.dto.Dto;
import cn.kgc.pojo.User;
import cn.kgc.util.DtoUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/userinfo/")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @PostMapping(value = "queryuserlinkuser",headers ="token")
    public Dto queryUserLinkUser(@RequestBody LinkUserNameVo linkUserNameVo, HttpServletRequest request){
        String token=request.getHeader("token");
        if (!stringRedisTemplate.hasKey(token)){
            return DtoUtil.getFalseDto("用户未登录","200001");
        }
        User user= JSON.parseObject(stringRedisTemplate.opsForValue().get(token),User.class);
        return  userService.queryUserLinkUser(user,linkUserNameVo.getLinkUserName());
    }

    @PostMapping(value = "adduserlinkuser", headers ="token")
    public Dto addUserLinkUser(@RequestBody LinkUserVo linkUserVo,HttpServletRequest request){
        String token=request.getHeader("token");
        if (!stringRedisTemplate.hasKey(token)){
            return DtoUtil.getFalseDto("用户未登录","200001");
        }
        User user= JSON.parseObject(stringRedisTemplate.opsForValue().get(token),User.class);
        return  userService.addUserLinkUser(user,linkUserVo);
    }

    @PostMapping(value = "modifyuserlinkuser",headers ="token")
    public Dto modifyUserLinkUser(@RequestBody LinkUserVo linkUserVo,HttpServletRequest request){
        String token=request.getHeader("token");
        if (!stringRedisTemplate.hasKey(token)){
            return DtoUtil.getFalseDto("用户未登录","200001");
        }
        User user= JSON.parseObject(stringRedisTemplate.opsForValue().get(token),User.class);
        return  userService.modifyUserLinkUser(user,linkUserVo);
    }
    @GetMapping(value = "deluserlinkuser",headers = "token")
    public Dto delUserLinkUser(String[] ids,HttpServletRequest request){
        String token=request.getHeader("token");
        if (!stringRedisTemplate.hasKey(token)){
            return DtoUtil.getFalseDto("用户未登录","200001");
        }
        return userService.delUserLinkUser(ids);
    }
}
